// src/api/salesOrder.ts
import request from '@/utils/request'

export interface SalesListParams {
  soNo: string
}

export interface SalesOrderPayload {
  createdAt: string
  customerId: number
  soNo: string
  status: string
}

export interface SalesOrderItem {
  price: number
  qty: number
  skuId: number
  soId: number
  taxRate: number
}

/**
 * 销售订单列表
 * GET /api/sales/order/list
 * @param {SalesListParams} params
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":[...]}
 */
export function listSalesOrders(params: SalesListParams): Promise<any> {
  return request({
    url: '/api/sales/order/list',
    method: 'get',
    params,
  })
}

/**
 * 创建销售订单
 * POST /api/sales/order
 * @param {Object} data
 * @param {string} data.createdAt
 * @param {number} data.customerId
 * @param {string} data.soNo
 * @param {string} data.status
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function createSalesOrder(data: SalesOrderPayload): Promise<any> {
  return request({
    url: '/api/sales/order',
    method: 'post',
    data,
  })
}

/**
 * 添加销售订单明细
 * POST /api/sales/order/{soId}/items
 * @param {number|string} soId 销售订单ID
 * @param {Array<Object>} items 明细数组
 * @param {number} items[].price
 * @param {number} items[].qty
 * @param {number} items[].skuId
 * @param {number} items[].soId
 * @param {number} items[].taxRate
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function addSalesOrderItems(soId: number | string, items: SalesOrderItem[]): Promise<any> {
  return request({
    url: `/api/sales/order/${soId}/items`,
    method: 'post',
    data: items,
  })
}